/************************************************************************
Copyright (c) 2010, 2013, C.R. All rights reserved. 

E-mail: crtoyou@gmail.com; crtoyou@163.com

You can redistribute it and/or modify it under the terms of the
GNU Lesser General Public License as published by the
Free Software Foundation version 3 of the License.

This source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. if not, write to the Free Software Foundation, Inc., 
51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
************************************************************************/

#pragma once
#include <string>

namespace std {

class stringex : public string
{
public:
	stringex(const wstring& ws, UINT cp = CP_UTF8)
	{
		clear();
		int len = WideCharToMultiByte(
			cp, 0, 
			ws.data(), ws.size(), 
			0, 0, 0, 0
			);
		if (len < 0)
			return;
		resize(len);
		WideCharToMultiByte(
			cp, 0, 
			ws.data(), ws.size(), 
			_Bx._Buf, len, 0, 0
			);
	}

	inline void format(const char* format, ...)
	{
		va_list arglist;
		va_start(arglist, format);
		int len = _vscprintf(format, arglist) + 1;
		resize(len);
		vsprintf_s(_Bx._Buf, len, format, arglist);
		va_end(arglist);
	}

};

class wstringex : public wstring
{
public:
	wstringex(const string& cs, UINT cp = CP_UTF8)
	{
		clear();
		int len = MultiByteToWideChar(
			cp, 0, 
			cs.data(), cs.size(), 
			0, 0
			);
		if (len < 0)
			return;
		resize(len);
		MultiByteToWideChar(
			cp, 0, 
			cs.data(), cs.size(), 
			_Bx._Buf, len
			);
	}

	inline void format(const wchar_t* format, ...)
	{
		va_list arglist;
		va_start(arglist, format);
		int _bufsize = _vscwprintf(format, arglist) + 1;
		resize(_bufsize);
		vswprintf_s(_Bx._Buf, _BUF_SIZE, format, arglist);
		va_end(arglist);
	}

};

#ifdef _UNICODE
typedef wstringex tstring;
#else
typedef stringex tstring;
#endif

} //namespace std